package com.hxx.controller;

import com.github.pagehelper.PageInfo;
import com.hxx.entily.Result;
import com.hxx.pojo.Product;
import com.hxx.service.IProductService;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 去产品列表页面
     *
     * @return
     */
    @RequestMapping("/product_list")
    public String productList(String productName, Model model,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        List<Product> allProducts = productService.getAllProducts(productName, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allProducts);

        model.addAttribute("product_list", allProducts);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("productName", productName);
        return "product_list";
    }

    /**
     * 产品新增
     *
     * @return
     */
    @RequestMapping("/addProduct")
    @ResponseBody
    public Result addProduct(Product product) {
        Result<Object> result = new Result<>();
        int i = productService.addProduct(product);
        if (i > 0) {
            result.setMsg("新增成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("新增失败！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 产品删除
     *
     * @return
     */
    @RequestMapping("/delProduct")
    @ResponseBody
    public Result delProduct(Integer id) {
        Result<Object> result = new Result<>();
        int i = productService.deleteProductById(id);
        if (i > 0) {
            result.setMsg("删除成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("删除失败！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @RequestMapping("/delAll")
    @ResponseBody
    public Result delAll(String ids) {
        Result<Object> result = new Result<>();
        if (ids != null && !ids.trim().equals("")) {
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);
            int i = productService.deleteAll(idsList);
            if (i > 0) {
                result.setMsg("删除成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("删除失败！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请选择id！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 根据id查询产品
     *
     * @return
     */
    @RequestMapping("/getProductById")
    @ResponseBody
    public Result getProductById(Integer id) {
        Result<Object> result = new Result<>();
        if (id != null) {
            Product product = productService.getProductById(id);
            if (product != null) {
                result.setData(product);
                result.setMsg("成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("没有此产品！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请选择id！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 产品编辑
     *
     * @return
     */
    @RequestMapping("/editProduct")
    @ResponseBody
    public Result editProduct(Product product) {
        Result<Object> result = new Result<>();
        int i = productService.editProduct(product);
        if (i > 0) {
            result.setMsg("修改成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("修改失败！");
            result.setSuccess(false);
        }
        return result;
    }
}